﻿/*============================================
 * 类名 :ProveCommand
 * 描述 :
 *   
 * 创建时间: 2011/2/10 16:07:22
 * Blog:   http://home.cnblogs.com/xiangism
 *============================================*/
using System;
using System.Collections.Generic;

using System.Text;

namespace 命题证明
{
    /// <summary></summary>
    class ProveCommand : MathAssistLibrary.ICommand
    {
        string MathAssistLibrary.ICommand.Describe
        {
            get { return "命令名为prove。后接已知条件用逗号隔开，再接证明结论用分号隔开"; }
        }

        string MathAssistLibrary.ICommand.Excute( string cmd )
        {
            string[] ss = cmd.Split( new char[] { ';' } );
            if ( ss.Length == 1 )
                return "参数必须包括“;”号来区分已知条件和结论";
            if ( ss.Length >= 3 )
                return "参数中出现两个“；”的错误";
            List<string> conditions = new List<string>( ss[0].Split( new char[] { ',' } ) );
            try
            {
                ResultProve p = new ResultProve();
                List<ResultParameter> process = p.Prove( conditions, ss[1] );
                if ( process != null )
                {
                    string result = Format( "定律依据", "结论", "序号" );
                    for ( int i = 0; i < process.Count; i++ )
                    {
                        string previous = "";
                        for ( int j = 0; j < process[i].Previous.Count; j++ )
                        {
                            previous += process[i].Previous[j].ToString() + " ";
                        }
                        previous += process[i].LawName;
                        result += Format( previous, process[i].Content, i.ToString() );
                    }
                    return result;
                }
                else
                    return "这个结论还不能被证明，可能是因为条件不全或本程序还不完善";
            }
            catch ( ExpressionException )
            {
                return "表达式格式出错";
            }

        }
        string Format( string law, string result, string index )
        {
           return  string.Format( "{0,-10}{1,-30}{2,-3}\n", law, result, index );
        }

        string MathAssistLibrary.ICommand.Name
        {
            get { return "prove"; }
        }

    }
}
